﻿using FlyingHat.Model.Models;
using FlyingHat.Model.Models.Global;
using FlyingHat.Service.IService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FlyingHat.Web.Controllers
{
    /// <summary>
    /// 书架
    /// </summary>
    public class BookshelfController : BaseController
    {
        private readonly IBookshelfService bookshelfService;
        private readonly ILibraryService libraryService;

        public BookshelfController(IBookshelfService bookshelfService, ILibraryService libraryService)
        {
            this.bookshelfService = bookshelfService;
            this.libraryService = libraryService;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var sel = await libraryService.GetSelect();
            sel.Insert(0, new SelectModel { Text = "请选择", Value = "0" });
            ViewBag.SelectLibrary = sel.Select(i => new SelectListItem { Text = i.Text, Value = i.Value });
            return View();
        }
        [HttpGet]
        public async Task<IActionResult> GetBookshelfList(long libraryId, string? shelfName, int page, int limit)
        {
            var res = await bookshelfService.GetList(libraryId, shelfName, page, limit);
            return Json(res);
        }

        [HttpGet]
        public async Task<IActionResult> Add(long id = 0)
        {
            var sel = await libraryService.GetSelect();
            ViewBag.SelectLibrary = sel.Select(i => new SelectListItem { Text = i.Text, Value = i.Value, Selected = i.Value == id.ToString() });
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> AddOrUpdate(BookshelfModel model)
        => Json(await bookshelfService.AddOrUpdate(model).ConfigureAwait(false));

        [HttpPost]
        public async Task<IActionResult> Delete(long id)
         => Json(await bookshelfService.Delete(id).ConfigureAwait(false));


        [HttpGet]
        public async Task<IActionResult> GetList(long libraryId)
        {
            var res = await bookshelfService.GetList(libraryId);
            return Json(res);
        }


    }
}
